package leetcode.秋招.广;

import leetcode.剑指offer.pojo.ListNode;

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */
public class Main {

    public class Solution {
        /**
         * 处理链表交集
         * @param a ListNode类 a的头结点
         * @param b ListNode类 b的头结点
         * @return ListNode类
         */
        public ListNode solution (ListNode a, ListNode b) {
            // write code here
            ListNode head = new ListNode(-1);
            ListNode ah = a;
            ListNode bh = b;
            ListNode temp = head;
            while (ah != null && bh != null) {
                if (ah.val == bh.val) {
                    temp.next = new ListNode(ah.val);
                    ah = ah.next;
                    bh = bh.next;
                    temp = temp.next;
                } else if (bh.val > ah.val) {
                    bh = bh.next;
                } else {
                    ah = ah.next;
                }
            }
            return head.next;
        }
    }
}
